﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game : MonoBehaviour
{
    public Transform cardsRoot;
    public static Dictionary<int, Card> CardsMap = new Dictionary<int, Card>();
    private IEnumerator Start()
    {
        // init cards data
        Card[] cards = cardsRoot.GetComponentsInChildren<Card>(true);
        for (int i = 0; i < cards.Length; i++)
        {
            cards[i].id = i;
            var rectTrans = cards[i].GetComponent<RectTransform>();

            cards[i].rect = new Rect(rectTrans.position, rectTrans.sizeDelta);
            cards[i].cardType = CardTypeCfg[Random.Range(0, 3)];
            
            CardsMap.Add(cards[i].id, cards[i]);
            
            cards[i].gameObject.SetActive(false);
        }
        
        // calc overlay
        for (int i = 0; i < cards.Length; i++)
        {
            for (int j = i + 1; j < cards.Length; j++)
            {
                if (cards[i].rect.Overlaps(cards[j].rect))
                {
                    cards[i].blocked.Add(cards[j].id);
                    cards[j].block.Add(cards[i].id);
                }
            }
            cards[i].gameObject.SetActive(true);
            cards[i].Init();
            yield return new WaitForSeconds(0.1f);
        }
    }

    public static List<CardType> CardTypeCfg = new List<CardType>
    {
        new CardType {tid = 1, color = Color.white},
        new CardType {tid = 2, color = Color.red},
        new CardType {tid = 3, color = Color.yellow},
        new CardType {tid = 4, color = Color.blue},
    };
}

public class CardType
{
    public int tid;
    public Color color;
}
